// pages/dj/dj/dj.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wjnr:[],
    id:0,
  },
  loginForm:function(e)
  {
    var that=this;
    const db = wx.cloud.database();
    var myDate = new Date();
    var time = myDate.toLocaleString();
    var data = e.detail.value;
    console.log(data);
    for(var i=0;i<data.tm_count;i++)
    {
      console.log(data["tm_lx" + i]);
      if(data["tm_lx"+i] == '0')
      {
        if (data["tm_bd" + i]) {
          if (data["tm" + i] == '') {
            wx.showModal({
              content: '第'+(i+1)+'题不能为空!',
              showCancel: false,
            });
            return false;
          }
        }
      } else if (data["tm_lx" + i] == '1') {
        if (data["tm_bd" + i]) {
          if (data["tm" + i] == '') {
            wx.showModal({
              content: '第' + (i + 1) + '题不能为空!',
              showCancel: false,
            });
            return false;
          }
          else
          {
            if (data["tm" + i].length > parseInt(data["tm_zdxx" + i])) {
              wx.showModal({
                content: '第' + (i + 1) + '题最多只能选择' + parseInt(data["tm_zdxx" + i]) +'项!',
                showCancel: false,
              });
              return false;
            }
            console.log(data["tm" + i].length);
            console.log(parseInt(data["tm_zdxx" + i]));
            if (data["tm" + i].length < parseInt(data["tm_zxxx" + i])) {
              wx.showModal({
                content: '第' + (i + 1) + '题最少要选择' + parseInt(data["tm_zxxx" + i]) + '项!',
                showCancel: false,
              });
              return false;
            }
          }
        }
      } else if (data["tm_lx" + i] == '2') {
        if (data["tm_bd" + i]) {
          if (data["tm" + i] == '') {
            wx.showModal({
              content: '第' + (i + 1) + '题不能为空!',
              showCancel: false,
            });
            return false;
          }
        }
      }
    }
    if(app.globalData.now_cfdj)
    {
      db.collection('user_dt').where({
        openid: app.globalData.openid,
        wjbh:that.data.id,
      })
        .get({
          success: res => {
            console.log(res);
            db.collection('user_dt').add({
              data: {
                dj_json: data,
                openid: app.globalData.openid,
                add_time: time,
                wjbh: that.data.id,
                wjnr: that.data.wjnr,
                hidden:false,
              },
              success: res => {
                console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id);
                wx.showModal({
                  content: '答卷提交成功',
                  showCancel: false,
                  success: function () {
                    wx.redirectTo({
                      url: "/pages/dj/start/strat?id=" + that.data.id,
                    })
                  }
                });
              },
              fail: err => {
                console.error('[数据库] [新增记录] 失败：', err)
              }
            })
          },
          fail: err => {
            console.error('[数据库] [新增记录] 失败：', err);
            wx.showModal({
              content: '提交失败,请重试!',
              showCancel: false,
              success: function () {
              }
            });
            return false;
          }
        })
    }
    else
    {
      db.collection('user_dt').where({
        openid: app.globalData.openid,
        wjbh:that.data.id,
      })
      .get({
        success: res => {
          console.log(res);
          if(res.data.length > 0)
          {
            if (app.globalData.now_xgdj)
            {
              db.collection('user_dt').doc(res.data[0]._id).update({
                data: {
                  dj_json: data,
                  openid: app.globalData.openid,
                  add_time: time,
                  wjbh: that.data.id,
                  wjnr: that.data.wjnr,
                  hidden: false,
                },
                success: res => {
                  console.log('[数据库] [更新记录] 成功，记录 _id: ', res._id);
                  wx.showModal({
                    content: '答卷更新成功',
                    showCancel: false,
                    success: function () {
                      wx.redirectTo({
                        url: "/pages/dj/start/strat?id=" + that.data.id,
                      })
                    }
                  });
                },
                fail: err => {
                  console.error('[数据库] [更新记录] 失败：', err)
                }
              })
            }
            else
            {
              wx.showModal({
                content: '请勿重复提交答卷',
                showCancel: false,
                success: function () {
                }
              });
              return false;
            }
          }
          else{
            db.collection('user_dt').add({
              data: {
                dj_json: data,
                openid: app.globalData.openid,
                add_time: time,
                wjbh: that.data.id,
                wjnr: that.data.wjnr,
                hidden: false,
              },
              success: res => {
                console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id);
                wx.showModal({
                  content: '答卷提交成功',
                  showCancel: false,
                  success: function () {
                    wx.redirectTo({
                      url: "/pages/dj/start/strat?id=" + that.data.id,
                    })
                  }
                });
              },
              fail: err => {
                console.error('[数据库] [新增记录] 失败：', err)
              }
            })
          }
        },
        fail: err => {
          console.error('[数据库] [新增记录] 失败：', err);
          wx.showModal({
            content: '提交失败,请重试!',
            showCancel: false,
            success: function () {
            }
          });
          return false;
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var nowid = app.globalData.now_id;
    console.log(nowid);
    var now_wj = app.globalData.now_wj;
    if (now_wj.length <= 0) {
      wx.showModal({
        content: '获取问卷内容失败,请重试!',
        showCancel: false,
        success: function () {
          wx.navigateBack({ changed: true });//返回上一页  
        }
      });
      return false;
    }
    else {
      console.log(now_wj);
      this.setData({
        id: nowid,
        wjnr: now_wj,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})